<?php
/**
 * Created by PhpStorm.
 * User: tanghui
 * Date: 2017/7/10
 * Time: 上午9:06
 */

class Controller_Seller_Order extends Controller_Seller_Abstract{

    function showMessage($message, $msgType, $url = null)
    {
        if($msgType=="error"){
            echo "<script>alert('{$message}');";
        }else{
            echo "<script>";
        }
        if ($url) echo "location='{$url}';";
        echo "</script>";
    }

    public function managerAction(){
        global $_F;
        //$uid= $_F['uid'];
        $shopId=$_F['shop_id'];
        $status= FRequest::getInt("status");
        $status=empty($status)?2:3;
        $order_list = Service_Order::getOrderList($shopId,$status);
        $oProductModel = new FTable("orders_product",'','xhs_slave');
        $outputOrder=[];
        if($order_list){
            foreach($order_list as $key=>$o){
                //1待付款，2待接单，3商家已接单，4派送中, 5已完成, 6已取消
                $o['status_name']=Service_Order::getOrderStatusName($o['status']);
                $o['send_time']=date('Y-m-d H:s:i',strtotime("+30 minutes",strtotime($o['create_time'])));
                $outputOrder[$key]=$o;
                $oProducts=$oProductModel->where(array('o_id'=>$o['o_id']))->select();
                $outputOrder[$key]['products']=$oProducts;
            }
        }
        $this->assign("status", $status);
        $this->assign("menu", 1);
        $this->assign("order_list", $outputOrder);
        $this->display("order_list_one");
    }

    public function managerexAction(){
        global $_F;
        $shopId=$_F['shop_id'];
        $status= FRequest::getInt("status");
        $status=empty($status)?4:5;
        $order_list = Service_Order::getOrderList($shopId,$status);
        $oProductModel = new FTable("orders_product",'','xhs_slave');
        $outputOrder=[];
        if($order_list){
            foreach($order_list as $key=>$o){
                //1待付款，2待接单，3商家已接单，4派送中, 5已完成, 6已取消
                $o['status_name']=Service_Order::getOrderStatusName($o['status']);
                $o['send_time']=date('Y-m-d H:s:i',strtotime("+30 minutes",strtotime($o['create_time'])));
                $outputOrder[$key]=$o;
                $oProducts=$oProductModel->where(array('o_id'=>$o['o_id']))->select();
                $outputOrder[$key]['products']=$oProducts;
            }
        }
        $this->assign("status", $status);
        $this->assign("menu", 2);
        $this->assign("order_list", $outputOrder);
        $this->display("order_list_two");
    }

    public function receiveAction(){
        global $_F;
        $shopId=$_F['shop_id'];
        $oid= FRequest::getInt("oid");
        $orderModel = new FTable("orders",'','xhs_master');
        $oProductModel = new FTable("orders_product",'','xhs_slave');
        //调用配送下单接口，通知快递员配送
        $orderInfo=$orderModel->where(array('auto_id'=>$oid))->find();
        $oProducts=$oProductModel->where(['o_id'=>$orderInfo['o_id']])->select();
        $chain_shop = new FTable('chain_shop','','xhs_slave');
        $shopInfo = $chain_shop->where(array('id'=>$shopId))->find();
        $products=[];
        foreach($oProducts as $p){
            $products[]=$p['title'].'￥'.$p['price_new'].'x'.$p['paynum'].$p['unit'];
        }
        $productsStr=implode(',',$products);
        $data=array(
            'order_id'=>$orderInfo['o_id'],
            'city'=>$orderInfo['city'],
            'district'=>$orderInfo['county'],
            'create_time'=>$orderInfo['create_time'],
            'order_info'=>$productsStr,
            'order_extend_info'=>$orderInfo['remark'],
            'order_price'=>$orderInfo['pay_price']*100,
            'expected_fetch_time'=>'',
            'expected_finish_time'=>'',
            'supplier_name'=>$shopInfo['name'],
            'supplier_address'=>$shopInfo['province'].$shopInfo['city'].$shopInfo['address'],
            'supplier_phone'=>$shopInfo['phone'],
            'supplier_lat'=>empty($shopInfo['y'])?0:$shopInfo['y'],
            'supplier_lng'=>empty($shopInfo['x'])?0:$shopInfo['x'],
            'pay_type'=>0,
            'receiver_name'=>$orderInfo['contact'],
            'receiver_address'=>$orderInfo['address'],
            'receiver_phone'=>$orderInfo['phone'],
            'receiver_lat'=>empty($orderInfo['lat'])?0:$orderInfo['lat'],
            'receiver_lng'=>empty($orderInfo['lng'])?0:$orderInfo['lng'],
            'callback_url'=>FConfig::get("global.burl").'/public/cbbd',
            //'pid'=>$orderInfo['api_pid'],
            //'expense'=>$orderInfo['send_price'],
        );
        //print_r($orderInfo);exit;
        $res=Service_ApiBaidu::addOrder($data);
        if($res->respcd != '0000'){
            $this->error($res->resperr,"/seller/order/manager?status=3");
        }else{
            //保存物流ID
           // $orderModel = new FTable("orders",'','xhs_master');
            Service_Order::updateExpend(3,$oid);
            //$orderModel->update1(array('status'=>3,'ship_id'=>$res->data->ship_id,'recv_expend'=>time()),array('auto_id'=>$oid,'shop_id'=>$shopId));
            $this->success("已经接单","/seller/order/manager?status=3");
        }


    }
    public function bsendAction(){
        global $_F;
        $shopId=$_F['shop_id'];
        $oid= FRequest::getInt("oid");
       // $orderModel = new FTable("orders",'','xhs_slave');

        Service_Order::updateExpend(4,$oid);
        //保存物流ID
        //$orderModel = new FTable("orders",'','xhs_master');
        //$orderModel->update1(array('status'=>4,'make_expend'=>time()),array('auto_id'=>$oid,'shop_id'=>$shopId));
        $this->success("开始配送","/seller/order/managerex");


    }

    public function tipsAction(){
        global $_F;
        $shop_id = $_F['shop_id'];
        $result = Service_Order::orderPopQueue($shop_id);
        if($result['size'] >= 1){
            return FResponse::output(array('status'=>1,'msg'=>'收到新的订单啦','num'=>$result['size']));
        }else{
            return FResponse::output(array('status'=>0));
        }
    }

}